﻿using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Input;
using Jerry.DroidBuilder.Common;
using Jerry.DroidBuilder.Helpers;
using Microsoft.Win32;

namespace Jerry.DroidBuilder
{
    public class UpdateReferenceViewModel : ViewModelBase
    {
        private bool _isSearchOnly;
        private string _targetDirectory;
        private string _originalDefinition;
        private string _targetDefinition;

        private string _output;

        public UpdateReferenceViewModel()
        {
            BrowseDirectoryCommand = new DelegateCommand<string>(OnBrowseDirectoryCommand);
            BrowseOriginalDefinitionCommand = new DelegateCommand<string>(OnBrowseOriginalDefinitionCommand);
            BrowseTargetDefinitionCommand = new DelegateCommand<string>(OnBrowseTargetDefinitionCommand);

            SearchCommand = new DelegateCommand<string>(OnSearchCommand);
        }

        public ICommand BrowseDirectoryCommand { get; set; }

        public ICommand BrowseOriginalDefinitionCommand { get; set; }

        public ICommand BrowseTargetDefinitionCommand { get; set; }

        public ICommand SearchCommand { get; set; }

        public bool IsSearchOnly
        {
            get { return _isSearchOnly; }
            set
            {
                _isSearchOnly = value;
                OnPropertyChanged(() => IsSearchOnly);
            }
        }

        public string TargetDirectory
        {
            get { return _targetDirectory; }
            set
            {
                _targetDirectory = value;
                OnPropertyChanged(() => TargetDirectory);
            }
        }

        public string OriginalDefinition
        {
            get { return _originalDefinition; }
            set
            {
                _originalDefinition = value;
                OnPropertyChanged(() => OriginalDefinition);
            }
        }

        public string TargetDefinition
        {
            get { return _targetDefinition; }
            set
            {
                _targetDefinition = value;
                OnPropertyChanged(() => TargetDefinition);
            }
        }

        public string Output
        {
            get { return _output; }
            set
            {
                _output = value;
                OnPropertyChanged(() => Output);
            }
        }

        private void OnBrowseDirectoryCommand(string payload)
        {
            using (var dlg = new System.Windows.Forms.FolderBrowserDialog())
            {
                if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    TargetDirectory = dlg.SelectedPath;
                }
            }
        }

        private void OnBrowseOriginalDefinitionCommand(string payload)
        {
            var dlg = new OpenFileDialog();

            if (dlg.ShowDialog() == true)
            {
                OriginalDefinition = dlg.FileName;
            }
        }

        private void OnBrowseTargetDefinitionCommand(string payload)
        {
            var dlg = new OpenFileDialog();

            if (dlg.ShowDialog() == true)
            {
                TargetDefinition = dlg.FileName;
            }
        }

        private void OnSearchCommand(string payload)
        {
            if (!Directory.Exists(TargetDirectory) || !File.Exists(OriginalDefinition) || !File.Exists(TargetDefinition))
            {
                MessageBox.Show("Please select directory and resource definition file.");
                return;
            }

            new Thread(new ThreadStart(delegate
            {
                var results = UpdateReferenceHelper.UpdateReference(TargetDirectory, OriginalDefinition, TargetDefinition, IsSearchOnly);

                UpdateOutput(results);
            }))
            {
                IsBackground = true
            }.Start();
        }

        private void UpdateOutput(IEnumerable<ResourceReference> results)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("Update result:\r\n");

            foreach (var item in results)
            {
                sb.AppendFormat("{0,-12}{1,-40}\t0x{2,-12:X8}\t{3}\r\n", item.Type, item.Name, item.Value, item.UpdatedValue == 0 ? "----------" : "0x" + item.UpdatedValue.ToString("X8"));
            }

            Output = sb.ToString();
        }
    }
}
